package com.shardings.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shardings.dao.TestUserDao;
import com.shardings.entity.UserDTO;
import com.shardings.utils.RandomAddressUtils;
import com.shardings.utils.RandomNameUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.infra.hint.HintManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author: LCG
 * @date: 2022-11-09 16:14:11
 * @description:
 **/
@Service
@Slf4j
public class TestShardingService extends ServiceImpl<TestUserDao,UserDTO> implements TestService {

    @Autowired
    private TestUserDao testUserDao;

    /**
     * @author: LCG
     * @date: 2022-11-09 16:17:22
     * @description:  将数据导入到分库分表的表中
     */
    public Object insertToShardingDataBase(){

        return null;
    }


    @Override
    public void insertUserDto(){

        Long[] prices=new Long[]{10L,12L,14L,5L,6L,20L,18L,16L,12L,8L};
        List<UserDTO> list=new ArrayList<>();
        for (int i=0;i<80;i++){
            UserDTO userDTO=new UserDTO();
            //userDTO.setId(Long.valueOf(i+""));
            String randomName = RandomNameUtils.getRandomName();
            userDTO.setUserName(randomName);
            userDTO.setPath(RandomAddressUtils.getRandomAddress());
            int nextInt = new Random().nextInt(3);
            userDTO.setBuMen("部门-"+nextInt);
            userDTO.setCreateTime(new Date());
            userDTO.setLoginName(randomName);
            userDTO.setSex(RandomNameUtils.getEnligshSex());
            userDTO.setSalt("12355");

            int i1 = i % 10;
            userDTO.setPrice(Long.valueOf((i+1)+""));
            //list.add(userDTO);

            boolean save = save(userDTO);
            log.info(save+"=================>"+Long.valueOf((i+1)+""));
        }

       // testUserDao.insertList(list);
    }

    @Override
    public Object selectByName(String userName){
        List<UserDTO> userList = testUserDao.getUserList(userName);
        return userList;
    }

    @Override
    public Object selectByName2(String userName, String buMen) {
        return testUserDao.getUserList2(userName,buMen);
    }

    @Override
    public Object complexList(String userName, String buMen,Long id) {
        return testUserDao.getUserList3(userName,buMen,id);
    }

    @Override
    public Object getListData() {

        HintManager instance = HintManager.getInstance();
        instance.setDatabaseShardingValue(0);
        //分库不分表的时候该API设置使用的数据库
        //instance.setDataSourceName("d0");

        //addDatabaseShardingValue 设置使用的数据库分片的标识信息 可以调用多次就是设置多个分片库
        //比如有数据库  d0 d1 d2 三个库 设置 0 1 就会传递这两分片的值到  HintShardingAlgorithm 这个接口的dosharding方法中
        instance.addDatabaseShardingValue("m_user",0L);
        instance.addDatabaseShardingValue("m_user",1L);
        //设置数据库分片的值和设置数据库分片的值是同理
        instance.addTableShardingValue("m_user",0L);
        //配置读写分离的话还API表示从写库上读数据，避免同步数据的延迟
        instance.setWriteRouteOnly();

        List<UserDTO> userList = testUserDao.getUserList("谷");

        return userList;
    }


    public Object getListData1() {

        HintManager instance = HintManager.getInstance();

        //addDatabaseShardingValue 设置使用的数据库分片的标识信息 可以调用多次就是设置多个分片库
        //比如有数据库  d0 d1 d2 三个库 设置 0 1 就会传递这两分片的值到  HintShardingAlgorithm 这个接口的dosharding方法中
        instance.addDatabaseShardingValue("m_user",0L);
        instance.addDatabaseShardingValue("m_user",1L);
        //设置数据库分片的值和设置数据库分片的值是同理
        instance.addTableShardingValue("m_user",0L);

        List<UserDTO> userList = testUserDao.getUserList("谷");
        return userList;
    }

}
